﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using eschoolBO;

namespace ESCHOOL.teachers
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        attend at = new attend();
        Classes cl = new Classes();
        student stud = new student();
        teacher teach = new teacher();
        protected void Page_Load(object sender, EventArgs e)
        {
            btnupdate.Visible = false;
            tbreason.Visible = false;
            RadioButtonList2.Visible = false;
            labreason.Visible = false;
            lableave.Visible = false;
            if(!IsPostBack)
            {
                DataTable dtt = teach.GetRecords(-99, "", "", "", "", "", "", -99, -99, -99, "", "", "", "", "", false, " and ");
                DataTable dtc = cl.GetRecords(-99, "", -99, false, " and ");
                if (Convert.ToInt64(Session["userid"].ToString()) == Convert.ToInt64(dtc.Rows[0]["teacher_id"].ToString()))
                {
                    Panel3.Visible = true; Panel4.Visible = false;
                    DataTable dt1 = cl.GetRecords(-99, "", Convert.ToInt64(Session["userid"]), false, " and ");
                    DataTable dt = at.GetRecords(-99, "", Convert.ToInt64(dt1.Rows[0]["class_id"].ToString()), -99, "a", "", true, false, " and ");
                    GridView1.DataSource = dt;
                    GridView1.DataBind();

                    DataTable dta = at.GetRecords(-99, "", Convert.ToInt64(dt1.Rows[0]["class_id"].ToString()), -99, "p", "", false, false, " and ");
                    GridView2.DataSource = dta;
                    GridView2.DataBind();

                    DataTable dtb = at.GetRecords(-99, "", Convert.ToInt64(dt1.Rows[0]["class_id"].ToString()), -99, "a", "", false, false, " and ");
                    GridView3.DataSource = dtb;
                    GridView3.DataBind();

                    tbclass.Text = dt1.Rows[0]["class_id"].ToString();
                    tbdate.Text = DateTime.Now.ToString();
                    DataTable dt2 = stud.GetRecords(-99, "", -99, Convert.ToInt64(tbclass.Text), "", "", "", "", "", "", -99, -99, "", -99, "", "", "", "", false, " and ");
                    ddstud.DataSource = dt2;
                    ddstud.DataTextField = "student_name";
                    ddstud.DataValueField = "student_id";
                    ddstud.DataBind();

                    
                }
                else
                {
                    Panel4.Visible = true; Panel3.Visible = false;
                    DataTable dtcl = cl.GetRecords(-99, "", -99, false, " and ");
                    ddclasses.DataSource = dtcl;
                    ddclasses.DataTextField = "class_name";
                    ddclasses.DataValueField = "class_id";
                    ddclasses.DataBind();
                    ddclasses.Items.Insert(0, new System.Web.UI.WebControls.ListItem("-Select-", "-99"));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                }


                   //DataTable dt1 = cl.GetRecords(-99, "", Convert.ToInt64(Session["userid"]), false, " and ");
                   //DataTable dt = at.GetRecords(-99, "", Convert.ToInt64(dt1.Rows[0]["class_id"].ToString()), -99, "a", "", true, false, " and ");
                   // GridView1.DataSource = dt;
                   // GridView1.DataBind();

                   // DataTable dta = at.GetRecords(-99, "", Convert.ToInt64(dt1.Rows[0]["class_id"].ToString()), -99, "p", "", false, false, " and ");
                   // GridView2.DataSource = dta;
                   // GridView2.DataBind();

                   // DataTable dtb = at.GetRecords(-99, "", Convert.ToInt64(dt1.Rows[0]["class_id"].ToString()), -99, "a", "", false, false, " and ");
                   // GridView3.DataSource = dtb;
                   // GridView3.DataBind();
               
                //tbclass.Text = dt1.Rows[0]["class_id"].ToString();
                //tbdate.Text=DateTime.Now.ToString();
                //DataTable dt2 = stud.GetRecords(-99, "", -99, Convert.ToInt64(tbclass.Text), "", "", "", "", "", "", -99, -99, "", -99, "", "", "", "", false, " and ");
                //ddstud.DataSource = dt2;
                //ddstud.DataTextField = "student_name";
                //ddstud.DataValueField = "student_id";
                //ddstud.DataBind();

                //DataTable dtcl = cl.GetRecords(-99, "", -99, false, " and ");
                //ddclasses.DataSource = dtcl;
                //ddclasses.DataTextField = "class_name";
                //ddclasses.DataValueField = "class_id";
                //ddclasses.DataBind();
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            DataTable dt1 = cl.GetRecords(-99, "", Convert.ToInt64(Session["userid"]), false, " and ");
            DataTable dt = at.GetRecords(-99, "", Convert.ToInt64(dt1.Rows[0]["class_id"].ToString()), -99, "", "", false, false, " and ");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void btnedit_Command(object sender, CommandEventArgs e)
        {

            btnupdate.Visible = true;
            btninsert.Visible = false;
            labreason.Visible = true;
            lableave.Visible = true;
            DataTable dt = at.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, "a", "", true, false, " and ");
            tbdayid.Text = dt.Rows[0]["day_id"].ToString();
            tbdate.Text = dt.Rows[0]["date"].ToString();
            tbclass.Text = dt.Rows[0]["class_id"].ToString();
            ddstud.SelectedValue = dt.Rows[0]["student_id"].ToString();
            // ddsubject.SelectedValue = dt.Rows[0]["subject_id"].ToString();
            RadioButtonList1.SelectedValue = dt.Rows[0]["attendence"].ToString();
            if (RadioButtonList1.SelectedValue == "a")
            {
                labreason.Visible = true;
                tbreason.Visible = true;
                lableave.Visible = true;
                RadioButtonList2.Visible = true;
            }

            tbreason.Text = dt.Rows[0]["reason"].ToString();
            RadioButtonList2.SelectedValue = dt.Rows[0]["leave"].ToString();
        }

        protected void btninsert_Click(object sender, EventArgs e)
        {
            at.day_id = -99;
            at.date=tbdate.Text;
            at.class_id = Convert.ToInt64(tbclass.Text);
            at.student_id=Convert.ToInt64(ddstud.SelectedValue.ToString());
            at.attendence=RadioButtonList1.SelectedValue.ToString();
             if (at.attendence.Equals("p"))
            {
                tbreason.Visible = false;
                RadioButtonList2.Visible = false;
                tbreason.ReadOnly = true;
                at.reason = tbreason.Text;
                at.leave = false;
            }
            else
            {
                at.reason = tbreason.Text;
                at.leave = Convert.ToBoolean(RadioButtonList2.SelectedValue.ToString());
            }
            at.isdeleted = false;
            at.SaveRecords();
            Response.Redirect("~/teachers/TAttendence.aspx");
        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            at.day_id = Convert.ToInt64(tbdayid.Text);
            at.date = tbdate.Text;
            at.class_id = Convert.ToInt64(tbclass.Text);
            at.student_id = Convert.ToInt64(ddstud.SelectedValue.ToString());
            //at.subject_id = Convert.ToInt64(ddsubject.SelectedValue.ToString());
            at.attendence = RadioButtonList1.SelectedValue.ToString();
            at.reason = tbreason.Text;
            at.leave = Convert.ToBoolean(RadioButtonList2.SelectedValue.ToString());
            at.isdeleted = false;
            at.SaveRecords();
            Response.Redirect("~/teachers/TAttendence.aspx");
        }

        protected void btndelete_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = at.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, "a", "", true, false, " and ");
                at.day_id = Convert.ToInt64(dt.Rows[0]["day_id"].ToString());
                at.date = dt.Rows[0]["date"].ToString();
                at.class_id = Convert.ToInt64(dt.Rows[0]["class_id"].ToString());
                at.student_id = Convert.ToInt64(dt.Rows[0]["student_id"].ToString());
                //at.subject_id = Convert.ToInt64(dt.Rows[0]["subject_id"].ToString());
                at.attendence = dt.Rows[0]["attendence"].ToString();
                at.reason = dt.Rows[0]["reason"].ToString();
                at.leave = Convert.ToBoolean(dt.Rows[0]["leave"].ToString());
                at.isdeleted = true;
                at.SaveRecords();
                Response.Redirect("~/teachers/TAttendence.aspx");
            }
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnupdate.Visible = true;
            if (RadioButtonList1.SelectedValue == "a")
            {
                tbreason.Visible = true;
                RadioButtonList2.Visible = true;
                labreason.Visible = true;
                lableave.Visible = true;
                
            }
        }

        protected void RadioButtonList2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void btneditp_Command(object sender, CommandEventArgs e)
        {
            btnupdate.Visible = true;
            btninsert.Visible = false;
            labreason.Visible = false;
            lableave.Visible = false;
            DataTable dt = at.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, "p", "", false, false, " and ");
            tbdayid.Text = dt.Rows[0]["day_id"].ToString();
            tbdate.Text = dt.Rows[0]["date"].ToString();
            tbclass.Text = dt.Rows[0]["class_id"].ToString();
            ddstud.SelectedValue = dt.Rows[0]["student_id"].ToString();
            // ddsubject.SelectedValue = dt.Rows[0]["subject_id"].ToString();
            RadioButtonList1.SelectedValue = dt.Rows[0]["attendence"].ToString();
            if (RadioButtonList1.SelectedValue == "a")
            {
                labreason.Visible = true;
                tbreason.Visible = true;
                lableave.Visible = true;
                RadioButtonList2.Visible = true;
            }
            tbreason.Text = dt.Rows[0]["reason"].ToString();
            RadioButtonList2.SelectedValue = dt.Rows[0]["leave"].ToString();


        }

        protected void btndeletep_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = at.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, "p", "", false, false, " and ");
                at.day_id = Convert.ToInt64(dt.Rows[0]["day_id"].ToString());
                at.date = dt.Rows[0]["date"].ToString();
                at.class_id = Convert.ToInt64(dt.Rows[0]["class_id"].ToString());
                at.student_id = Convert.ToInt64(dt.Rows[0]["student_id"].ToString());
                //at.subject_id = Convert.ToInt64(dt.Rows[0]["subject_id"].ToString());
                at.attendence = dt.Rows[0]["attendence"].ToString();
                at.reason = dt.Rows[0]["reason"].ToString();
                at.leave = Convert.ToBoolean(dt.Rows[0]["leave"].ToString());
                at.isdeleted = true;
                at.SaveRecords();
                Response.Redirect("~/teachers/TAttendence.aspx");
            }
        }

        protected void btnedit_Command1(object sender, CommandEventArgs e)
        {
            btnupdate.Visible = true;
            btninsert.Visible = false;
            labreason.Visible = false;
            lableave.Visible = false;
            DataTable dt = at.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, "a", "", false, false, " and ");
            tbdayid.Text = dt.Rows[0]["day_id"].ToString();
            tbdate.Text = dt.Rows[0]["date"].ToString();
            tbclass.Text = dt.Rows[0]["class_id"].ToString();
            ddstud.SelectedValue = dt.Rows[0]["student_id"].ToString();
            // ddsubject.SelectedValue = dt.Rows[0]["subject_id"].ToString();
            RadioButtonList1.SelectedValue = dt.Rows[0]["attendence"].ToString();
            if (RadioButtonList1.SelectedValue == "a")
            {
                labreason.Visible = true;
                tbreason.Visible = true;
                lableave.Visible = true;
                RadioButtonList2.Visible = true;
            }
            tbreason.Text = dt.Rows[0]["reason"].ToString();
            RadioButtonList2.SelectedValue = dt.Rows[0]["leave"].ToString();

        }

        protected void btndelete_Command1(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = at.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, "a", "", false, false, " and ");
                at.day_id = Convert.ToInt64(dt.Rows[0]["day_id"].ToString());
                at.date = dt.Rows[0]["date"].ToString();
                at.class_id = Convert.ToInt64(dt.Rows[0]["class_id"].ToString());
                at.student_id = Convert.ToInt64(dt.Rows[0]["student_id"].ToString());
                //at.subject_id = Convert.ToInt64(dt.Rows[0]["subject_id"].ToString());
                at.attendence = dt.Rows[0]["attendence"].ToString();
                at.reason = dt.Rows[0]["reason"].ToString();
                at.leave = Convert.ToBoolean(dt.Rows[0]["leave"].ToString());
                at.isdeleted = true;
                at.SaveRecords();
                Response.Redirect("~/teachers/TAttendence.aspx");
            }
        }

        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView2.PageIndex = e.NewPageIndex;
            DataTable dt1 = cl.GetRecords(-99, "", Convert.ToInt64(Session["userid"]), false, " and ");
            DataTable dta = at.GetRecords(-99, "", Convert.ToInt64(dt1.Rows[0]["class_id"].ToString()), -99, "p", "", false, false, " and ");
            GridView2.DataSource = dta;
            GridView2.DataBind();
            
        }

        protected void GridView3_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView3.PageIndex = e.NewPageIndex;
            DataTable dt1 = cl.GetRecords(-99, "", Convert.ToInt64(Session["userid"]), false, " and ");
            DataTable dtb = at.GetRecords(-99, "", Convert.ToInt64(dt1.Rows[0]["class_id"].ToString()), -99, "a", "", false, false, " and ");
            GridView3.DataSource = dtb;
            GridView3.DataBind();
               
        }

        protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void GridView3_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView3_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void ddclasses_SelectedIndexChanged(object sender, EventArgs e)
        {
            Label1.Visible = true; Label2.Visible = true; Label3.Visible = true;
            DataTable dtx = at.GetRecords(-99, "", Convert.ToInt64(ddclasses.SelectedValue.ToString()), -99, "p", "", false, false, " and ");
            GridView4.DataSource = dtx;
            GridView4.DataBind();

            DataTable dty = at.GetRecords(-99, "", Convert.ToInt64(ddclasses.SelectedValue.ToString()), -99, "a", "", true, false, " and ");
            GridView5.DataSource = dty;
            GridView5.DataBind();

            DataTable dtz= at.GetRecords(-99, "", Convert.ToInt64(ddclasses.SelectedValue.ToString()), -99, "a", "", false, false, " and ");
            GridView6.DataSource = dtz;
            GridView6.DataBind();
        }
    }
}
